<?php
namespace app\techn_api\controller;
use think\Controller;
use think\Db;
class Base extends Controller
{
	// 输入参数 post|get|request
	protected $data_post;
	protected $data_get;
	protected $data_request;

	// 用户信息
	protected $user;

	// 技师信息
	protected $techn;

	public function _initialize()
	{
		// 输入参数
		$this->data_post = input('post.');
		$this->data_get = input('get.');
		$this->data_request = input();

		// 公共数据初始化
		$this->CommonInit();
	}

	/**
	 * 公共数据初始化
	 */
	public function CommonInit()
	{
		$headers = request()->header();
		if(!empty($headers['token']))
		{
			//请求身份
			$identity_type = IdentityType();
			//当前应用平台
			$client_type = ApplicationClientType();

			$user_token_info = Db::name('user_token')->where(['identity_type' => $identity_type, 'client_type' => $client_type, 'token' => $headers['token']])->find();
			if(!empty($user_token_info))
			{
				$user = Db::name('user')->where(['id' => $user_token_info['user_id']])->find();
				if(!empty($user))
				{
					//获取用户openid
					$user['weixin_openid'] = Db::name('user_weixin')->where(['user_id' => $user['id'], 'identity_type' => 'techn', 'client_type' => 'weixin'])->value('openid');
					$this->user = $user;
					//获取技师信息
					$techn = Db::name('techn')->where('user_id=:user_id')->bind(['user_id' => $user['id']])->find();
					if(!empty($techn))
					{
						$this->techn = $techn;
					}
				}
			}
		}
	}

	/**
	 * 登录校验
	 */
	protected function IsLogin()
    {
        if(empty($this->user))
        {
            exit(json_encode(['code' => -400, 'msg' => '登录失效，请重新登录']));
        }
    }

    /**
     * 技师校验
     */
    protected function IsTechn()
    {
    	if(empty($this->techn))
    	{
    		exit(json_encode(['code' => -500, 'msg' => '您还不是技师，请申请']));
    	}
    }
}